﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using HCQ2_Common;
using System.Web;

namespace HCQ2UI_Logic.BaseController
{
    /// <summary>
    /// 获取语言文件数据
    /// </summary>
    public class LangController: BaseLogic
    {
        /// <summary>
        /// 返回语言文件JSON数据，对象名称“Lang.xxx”
        /// </summary>
        /// <returns></returns>
        [Description("获取语言文件")]
        [HCQ2_Common.Attributes.Skip]
        public JavaScriptResult GetLangData()
        {
            string LangJson = LangHelper.GetJsonWrite();
            return this.JavaScript(LangJson);
        }

        /// <summary>
        /// 设置语言
        /// </summary>
        /// <param name="LangName"></param>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Skip]
        public ActionResult SetLang(string LangName)
        {
            if (string.IsNullOrEmpty(LangName))
                LangName = GetDefaultLanuage();
            LangHelper.Init(LangName,false);
            SetCookieValue(LangName);
            return operateContext.RedirectAjax(0, "设置语言成功", "", "");
        }

        //设置cookie
        private void SetCookieValue(string LangName)
        {
            HttpCookie _cookie = new HttpCookie("Language", LangName);
            _cookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(_cookie);
        }
        //获取默认语言
        private string GetDefaultLanuage()
        {
            //登录信息
            var langHeader = string.Empty;
            if (operateContext.Usr != null)
            {
                string tempLang = operateContext.Usr.lang;
                if (string.IsNullOrEmpty(tempLang))
                    langHeader = "cn";
                else
                {
                    if (tempLang.Equals("cn-CN"))
                        langHeader = "cn";
                    else if (tempLang.Equals("en-US"))
                        langHeader = "en";
                    else
                        langHeader = tempLang;
                }
            }
            else
            {
                var cookie = Request.Cookies["Language"];
                if (cookie != null)
                    langHeader = cookie.Value.ToString();
                else
                    langHeader = "cn";
            }
            return langHeader;
        }
    }
}
